// 路由配置优化版本 - 精简版
import setting from '@/setting'

// 动态布局选择
const managementLayout = () => import('@/layout/index.vue')
const largeScreenLayout = () => import('@/layoutLargeScreen/index.vue')
const Layout = setting.isLargeScreen ? largeScreenLayout : managementLayout

// 路由元数据接口定义
export interface RouteMeta {
     title: string
     hidden?: boolean
     icon?: string
     keepAlive?: boolean
     parentPath?: string
     [key: string]: any // 支持扩展属性
}

// 路由配置接口
export interface RouteConfig {
     path: string
     component?: any
     name?: string
     meta?: RouteMeta
     redirect?: string
     children?: RouteConfig[]
}

// 基础路由配置 - 仅保留必要页面
export const constantRoute: RouteConfig[] = [
     // 核心布局和首页
     {
          path: '/',
          component: Layout,
          name: 'layout',
          meta: {
               title: '首页',
               icon: 'iconfont icon-shouye',
          } as RouteMeta,
          redirect: '/home',
          children: [
               {
                    path: '/home',
                    component: () => import('@/views/home/index.vue'),
                    name: 'home',
                    meta: {
                         title: '首页',
                         hidden: false,
                         icon: 'iconfont icon-shouye',
                         parentPath: '/',
                    } as RouteMeta,
               },
               {
                    path: '/epicDesigner',
                    component: () => import('@/views/epicDesigner/index.vue'),
                    name: 'epicDesigner',
                    meta: {
                         title: '表单拖拽',
                         hidden: false,
                         icon: 'iconfont icon-shouye',
                         parentPath: '/',
                    } as RouteMeta,
               },
          ],
     },
     // 大屏展示
     {
          path: '/largeScreen',
          component: () => import('@/views/largeScreen/index.vue'),
          name: 'largeScreen',
          meta: {
               title: '大屏展示',
               hidden: false,
               icon: 'iconfont icon-shujuzhexiantu',
          },
     },

     // 用户认证路由
     {
          path: '/login',
          component: () => import('@/views/login/index.vue'),
          name: 'login',
          meta: {
               title: '登录',
               hidden: true,
               icon: 'Promotion',
          } as RouteMeta,
     },

     // 404页面
     {
          path: '/404',
          component: () => import('@/views/404/index.vue'),
          name: '404',
          meta: {
               title: '404页面',
               hidden: true,
               icon: 'iconfont icon-404',
          } as RouteMeta,
     },
     // 未找到路由
     {
          path: '/:pathMatch(.*)*',
          redirect: '/404',
          name: 'Any',
          meta: {
               title: '任意路由',
               hidden: true,
               icon: 'DataLine',
          } as RouteMeta,
     },
]

// 异步路由配置（预留扩展）
export const asyncRoute: any[] = []

// 通配符路由 - 404页面处理
export const anyRoute = {
     path: '/:pathMatch(.*)*',
     redirect: '/404',
     name: 'Any',
     meta: {
          title: '任意路由',
          hidden: true,
          icon: 'DataLine',
     } as RouteMeta,
}

// 路由工具函数
export const routeUtils = {
     /**
      * 获取所有路由配置
      * @returns 合并后的完整路由配置
      */
     getAllRoutes: () => {
          return [...constantRoute, ...asyncRoute, anyRoute]
     },

     /**
      * 根据名称查找路由
      * @param name 路由名称
      * @returns 路由配置或undefined
      */
     findRouteByName: (name: string) => {
          const allRoutes = routeUtils.getAllRoutes()
          return allRoutes.find((route) => route.name === name)
     },

     /**
      * 获取可见路由（非隐藏）
      * @returns 可见路由列表
      */
     getVisibleRoutes: () => {
          return constantRoute.filter((route) => !route.meta?.hidden)
     },

     /**
      * 获取面包屑路径
      * @param currentPath 当前路径
      * @returns 面包屑数组
      */
     getBreadcrumb: (currentPath: string) => {
          const breadcrumb = []
          const segments = currentPath.split('/').filter(Boolean)

          let path = ''
          for (const segment of segments) {
               path += `/${segment}`
               const route = routeUtils.findRouteByPath(path)
               if (route && !route.meta?.hidden) {
                    breadcrumb.push({
                         title: route.meta?.title || segment,
                         path: route.path,
                    })
               }
          }

          return breadcrumb
     },

     /**
      * 根据路径查找路由
      * @param path 路由路径
      * @returns 路由配置或undefined
      */
     findRouteByPath: (path: string) => {
          const allRoutes = routeUtils.getAllRoutes()
          return allRoutes.find((route) => route.path === path)
     },
}
